package org.txazo.blog.web.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.txazo.blog.commons.cache.Cache;
import org.txazo.blog.util.ContextUtil;
import org.txazo.blog.web.action.base.BaseAction;

/**
 * 验证码 - Version1.0
 * 
 * @author Tu XiaoZhou
 * @since 2013-08-10
 * 
 */
@Controller
@Scope("session")
@Namespace("/image")
public class AuthCodeImageActionV1 extends BaseAction {

	private static final long serialVersionUID = 1L;

	private static final String STRING_SLASH = "/";
	private static final String IMAGE_TYPE = "jpg";
	private static final String IMAGE_SUFFIX = ".jpg";

	@Autowired
	private Cache authCodeCache;

	/** 验证码图片URL */
	private String imageURL;

	/** 图片的宽度 */
	private static final int width = 100;
	/** 图片的高度 */
	private static final int height = 36;
	/** 验证码字符个数 */
	private static final int count = 4;
	/** x */
	private static int x = 0;
	/** y */
	private static int y = 0;
	/** 字体的高度 */
	private static int fontHeight = 0;

	/** 验证码字符 */
	private static final char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F',
			'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
			'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9' };

	static {
		x = (width - 10) / count;
		y = height - 4;
		fontHeight = height - 2;
	}

	/**
	 * 获取验证码
	 */
	@Action(value = "authCodeV1", results = { @Result(name = SUCCESS, type = "redirect", location = "${imageURL}") })
	public String authCode() {
		BufferedImage buffImg = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D gd = buffImg.createGraphics();

		Random random = new Random();

		/** 图像填充为白色 */
		gd.setColor(Color.WHITE);
		gd.fillRect(0, 0, width, height);

		/** 字体 */
		Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
		gd.setFont(font);

		/** 边框 */
		gd.setColor(Color.BLACK);
		gd.drawRect(0, 0, width - 1, height - 1);

		/** 干扰线 */
		gd.setColor(Color.BLACK);
		for (int i = 0; i < 160; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			gd.drawLine(x, y, x + xl, y + yl);
		}

		/** 随机产生的验证码 */
		StringBuffer randomCode = new StringBuffer();

		int red = 0;
		int green = 0;
		int blue = 0;

		for (int i = 0; i < count; i++) {
			String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
			red = random.nextInt(255);
			green = random.nextInt(255);
			blue = random.nextInt(255);

			gd.setColor(new Color(red, green, blue));
			gd.drawString(strRand, 5 + i * x, y);

			randomCode.append(strRand);
		}

		authCodeCache.set(getRemoteIP(), randomCode.toString());

		/** 输出图像 */
		try {
			String imagePath = ContextUtil.getWebPath() + "/images/authCode"
					+ STRING_SLASH + getRemoteIP() + IMAGE_SUFFIX;

			ImageIO.write(buffImg, IMAGE_TYPE, new File(imagePath));

			buffImg.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

		imageURL = "http://127.0.0.1/images/authCode" + STRING_SLASH
				+ getRemoteIP() + IMAGE_SUFFIX;

		return SUCCESS;
	}

	public String getImageURL() {
		return imageURL;
	}

	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}

}
